package com.text.config;



import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@ComponentScan("com.text")
//扫描mapper目录
@MapperScan("com.text.mapper")
//主配置类
@Configuration
public class Configration {

    //    第一种方式
//    编写datasource
    @Bean
    public DataSource dataSource(){
        PooledDataSource dataSource=new PooledDataSource();
//        加载驱动
        dataSource.setDriver("com.mysql.cj.jdbc.Driver");
//        编写url，注意这里的db10是我的数据库名
        dataSource.setUrl("jdbc:mysql://localhost:3306/db10?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8");
//        编写账号
        dataSource.setUsername("root");
//        编写密码
        dataSource.setPassword("123456");
        return dataSource;
    }

    //    编写SqlSessionFactoryBean
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired DataSource dataSource){
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean;

    }

}

